<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="include/common-header :: common_header(~{::title},~{::link})">
    <title>经费导入</title>
</head>
<body class="gray-bg">

<!--页面内容开始-->
<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-md-12">
            <div class="ibox ">
                <div class="ibox-content">

                    <div id="wizard">
                        <h1>上传待导入文件</h1>
                        <section class="step-content budgetUpload" data-mode="iframe"
                                 data-url="/web/money/cur/moneyinfodetail-import-upload">
                        </section>

                        <h1>处理数据</h1>
                        <section class="step-content">
                            <div class="row animated fadeInRight">
                                <div class="col-12">
                                    <div class="ibox ">
                                        <div class="ibox-title">
                                            <h5>未匹配到系统中的项目</h5>
                                            <div class="ibox-tools">
                                                <a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="saveProjectAll()">
                                                    全部新增
                                                </a>
                                            </div>
                                        </div>
                                        <div class="ibox-content">
                                            <div class="scroll_content">
                                                <ol id="inportDetail_project">

                                                </ol>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </section>

                        <!--<h1>导入经费</h1>
                        <section class="step-content">
                            <div class="text-center m-t-md">
                                <button id="moneyinfodetail-import-save-btn" type="button" class="btn btn-w-m btn-default" onclick="saveAll()">导入</button>
                                <div id="moneyinfodetail-import-save-success" class="alert alert-success hide">
                                    导入成功.
                                </div>
                            </div>
                        </section>-->

                        <!--<h1>上传结果</h1>
                        <section class="step-content">
                            <div class="text-center m-t-md">

                            </div>
                        </section>-->
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
<!--页面内容结束-->

<div th:replace="include/common-js :: common_js"></div>
<script type="text/javascript">
    $(document).ready(function () {
        moneyinfodetailImportObj.initWizard();

        $(".budgetUpload").find("iframe").on("load", function (event) {//判断 iframe是否加载完成
            moneyinfodetailImportObj.uploadAreaHeight = $(this).contents().height();
            console.log("ab",$(this))
            $(this).height($(this).contents().height())
        });

        $('.scroll_content').slimscroll({
            height: '200px'
        })
    });

    let moneyinfodetailImportObj = new Object({
        uploadSuccess: false,
        projectSuccess: false,
        myWizard: undefined,
        uploadAreaHeight: undefined,
        initWizard() {
            this.myWizard = $("#wizard").steps({
                bodyTag: "section",
                transitionEffect: "fade",
                labels: {
                    cancel: "Cancel",
                    current: "current step:",
                    pagination: "Pagination",
                    finish: "导入",
                    next: "下一步",
                    previous: "上一步",
                    loading: "加载中 ..."
                },
                onStepChanging: function (event, currentIndex, newIndex) {
                    if (newIndex < currentIndex) {
                        return true;
                    }
                    if (currentIndex == 0) {
                        return moneyinfodetailImportObj.uploadSuccess;
                    } else {

                    }
                },
                onStepChanged: function (event, currentIndex, priorIndex) {
                    if (currentIndex == 0) {
                        $(".budgetUpload").find("iframe").height($(".budgetUpload").find("iframe").contents().height())
                    }
                },
                onFinishing: function (event, currentIndex) {
                    // 进行一些校验
                    return moneyinfodetailImportObj.projectSuccess;
                },
                onFinished: function (event, currentIndex) {
                    saveAll();
                }
            });
        }
    });

    /**
     * 导入文件上传后的回调
     * @param success
     * @param msg
     */
    function uploadOver(success, msg) {
        if (success) {
            moneyinfodetailImportObj.uploadSuccess = true;
            getNoProject();
            $("#wizard").steps("next");
        } else {
            swal({
                title: "操作失败",
                text: msg,
                type: "error",
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                closeOnConfirm: false
            });
        }
    }

    function getNoProject() {
        moneyinfodetailImportObj.projectSuccess = false;
        $.get("/money/cur/moneyinfodetail/noProjects", {}, function (data) {
            if (data.code == 0) {
                $('#inportDetail_project').empty();
                if (data.data.length == 0) {
                    moneyinfodetailImportObj.projectSuccess = true;
                } else {
                    data.data.map(o => {
                        $(`<li>${o}</li>`).appendTo($('#inportDetail_project'));
                    })
                }
            }
        })
    }

    function saveProjectAll() {
        if(!moneyinfodetailImportObj.projectSuccess){
            $.post("/money/cur/moneyinfodetail/saveProjectAll", {}, function (data) {
                if (data.code == 0) {
                    getNoProject();
                }
            })
        }
    }

    function saveAll() {
        let remark = $("#importDetail_remarkForm").find("#importDetail_remark").val()
        $.post("/money/cur/moneyinfodetail/saveAll", {}, function (data) {
            if (data.code == 0) {
                swal({
                    title: "操作提示",
                    text: '导入成功',
                    type: "success",
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定",
                    closeOnConfirm: true
                }, function () {
                    moneyinfodetailImportObj.uploadSuccess = false;
                    moneyinfodetailImportObj.projectSuccess = false;
                    moneyinfodetailImportObj.myWizard.steps("previous");
                });
            } else {
                swal({
                    title: "操作失败",
                    text: data.msg,
                    type: "error",
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定",
                    closeOnConfirm: false
                });
            }
        })
    }


</script>
</body>
</html>